package io.itit.ecp.admin.provide.action.controller.miniprogram;


import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;
import icu.mhb.mybatisplus.plugln.extend.Joins;
import io.itit.ecp.admin.provide.action.req.MiniProgramCarouseImageQueryReq;
import io.itit.ecp.admin.provide.action.resp.MiniProgramCarouseImageResp;
import io.itit.ecp.admin.server.entity.MiniProgramCarouseImageEntity;
import io.itit.grass.common.provide.action.resp.ResultResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 小程序轮播图
 */
@Api(tags = {"小程序轮播图"})
@RestController
@RequestMapping("mini/carouseImage")
public class MiniCarouseImageController {


    @ApiOperation("列表查询")
    @PostMapping("queryList")
    public ResultResp<List<MiniProgramCarouseImageResp>> queryList(@RequestBody MiniProgramCarouseImageQueryReq req) {
        return ResultResp.success(buildQW(req).joinList(MiniProgramCarouseImageResp.class));
    }

    private static JoinLambdaWrapper<MiniProgramCarouseImageEntity> buildQW(MiniProgramCarouseImageQueryReq req) {
        return Joins.of(MiniProgramCarouseImageEntity.class)
                .eq(StringUtils.hasLength(req.getPosition()), MiniProgramCarouseImageEntity::getPosition, req.getPosition())
                .eq(MiniProgramCarouseImageEntity::getStatus, Boolean.TRUE)
                .like(StringUtils.hasLength(req.getName()), MiniProgramCarouseImageEntity::getName, req.getName())
                .orderByAsc(MiniProgramCarouseImageEntity::getSort)
                .orderByDesc(StringUtils.hasLength(req.getSortColumn()), MiniProgramCarouseImageEntity::getCreateTm);
    }
}
